/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paziente;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.NamingException;
import javax.servlet.jsp.jstl.sql.ResultSupport;


import connezione.PooledConnection;

import javax.faces.context.FacesContext;
import javax.servlet.jsp.jstl.sql.Result;
import login.BeanUser;

/**
 *
 * @author sestari
 */
public class BeanPaziente {

    PooledConnection conn;
    BeanUser user;
    private String passdamod;
    private String maildamod;

    /** Creates a new instance of BeanPaziente */
    public BeanPaziente() {
        conn = (PooledConnection) ((BeanUser) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user")).getCon();
        user = ((BeanUser) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user"));

    }

    public String getnome() {

        String e = ((BeanUser) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user")).getNome();
        return e;
    }

    public String modData() throws SQLException {

        Statement stmt = conn.createStatement();
        stmt.executeUpdate("UPDATE `db_user15`.`Utenti` SET `password`='" + passdamod + "' WHERE `username`='" + user.getUsername() + "'");

        stmt.executeUpdate("UPDATE `db_user15`.`Anagrafica` SET `e_mail`='" + maildamod + "' WHERE `id`='" + user.getId() + "'");



        return "back";
    }

    public Result getAnagrafica() throws SQLException, NamingException {
        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery("SELECT * FROM Persona WHERE id =  " + user.getId());
        setMaildamod(user.getEmail());
        setPassdamod(user.getPassword());
        return ResultSupport.toResult(result);
    }

    public Result getPadre() throws SQLException, NamingException {
        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery("SELECT nome, cognome, foto FROM Persona WHERE id =  " + user.getId_padre());
        setMaildamod(user.getEmail());
        setPassdamod(user.getPassword());
        return ResultSupport.toResult(result);
    }

    public Result getMadre() throws SQLException, NamingException {
        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery("SELECT nome, cognome, foto FROM Persona WHERE id =  " + user.getId_madre());
        setMaildamod(user.getEmail());
        setPassdamod(user.getPassword());
        return ResultSupport.toResult(result);
    }

    public Result getCapo() throws SQLException, NamingException {
        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery("SELECT nome, cognome, foto FROM Persona WHERE id =  " + user.getId_capo_vilaggio());
        setMaildamod(user.getEmail());
        setPassdamod(user.getPassword());
        return ResultSupport.toResult(result);
    }

    public Result getVisite() throws SQLException, NamingException {
        Statement stmt = conn.createStatement();
        String cmd = "SELECT *, IF(richiami=-1,\"color:red\",\"\") as ef,  IF( DATEDIFF(data_scadenza, now()) < 7  ,\"font-weight:bold\",\"\") as weekdiff FROM Visita WHERE id_paziente =  " + user.getId() + " and DATEDIFF(data_scadenza, now()) > 0";
        cmd += " UNION ";
        cmd += "SELECT *, IF(richiami=-1,\"color:red\",\"\") as ef,  IF( DATEDIFF(data_scadenza, now()) < 7  ,\"\",\"\") as weekdiff FROM Visita WHERE id_paziente =  " + user.getId() +" and DATEDIFF(data_scadenza, now()) < 0 ORDER BY data_scadenza desc";
        ResultSet result = stmt.executeQuery(cmd);
        return ResultSupport.toResult(result);
    }

    public Result getScadenze() throws SQLException, NamingException {
        Statement stmt = conn.createStatement();
        String cmd = "SELECT * FROM Visita WHERE id_paziente = " + user.getId() + " AND richiami >= 0 AND TIMEDIFF(data_scadenza, now()) < 0 ORDER BY TIMEDIFF(now(), data_scadenza) desc LIMIT 5";
      
        ResultSet result = stmt.executeQuery(cmd);
        return ResultSupport.toResult(result);
    }

    public String getLat() throws SQLException {
     String res = "";
         Statement stmt = conn.createStatement();
        String cmd = "SELECT latitudine FROM Anagrafica WHERE id = " + user.getId();
      
        ResultSet result = stmt.executeQuery(cmd);
        result.next();
        res = result.getString("latitudine");
        
        return res; }
    
    
     public String getLng() throws SQLException {
     
      String res = "";
         Statement stmt = conn.createStatement();
        String cmd = "SELECT longitudine FROM Anagrafica WHERE id= " + user.getId();
      
        ResultSet result = stmt.executeQuery(cmd);
         result.next();
        res = result.getString("longitudine");
        
        return res;
     }
    
    
    /**
     * @return the passdamod
     */
    public String getPassdamod() {
        return passdamod;
    }

    /**
     * @param passdamod the passdamod to set
     */
    public void setPassdamod(String passdamod) {
        this.passdamod = passdamod;
    }

    /**
     * @return the maildamod
     */
    public String getMaildamod() {
        return maildamod;
    }

    /**
     * @param maildamod the maildamod to set
     */
    public void setMaildamod(String maildamod) {
        this.maildamod = maildamod;
    }
}
